package com.itheima.mapper;

import com.github.pagehelper.Page;
import com.itheima.pojo.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface RoleMapper {

    @Select("select * from t_role  where id=#{id}")
    Role findById(Integer id);

    @Select("select * from t_role ,t_user_role where id=role_id and user_id=#{id}")
    @Results(id = "menuMap", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(property = "menus", column = "id", many = @Many(select = "com.itheima.mapper.MenuMapper.findMenuById")),
            @Result(property = "permissions", column = "id", many = @Many(select = "com.itheima.mapper.PermissionMapper.findPermissionById"))
    })
    Role findRoleById(Integer id);

    @Select("select * from t_role")
    List<Role> findAll();


    @Insert("insert into t_user_role value(#{userId},#{roleId})")
    void setUserAndRole(@Param("userId") Integer userId,@Param("roleId") Integer roleId);

    @Delete("delete from t_user_role where user_Id=#{id}")
    void detele(Integer id);

    Page<Role> findByCondition(String queryString);

    @Insert("insert into t_role_menu value(#{roleId},#{id})")
    void setRoleAndMenu(@Param("roleId") Integer roleId, @Param("id") Integer id);

    @Insert("insert into t_role_permission value(#{roleId},#{id})")
    void setRoleAndPermission(@Param("roleId") Integer roleId, @Param("id") Integer id);
    @Insert("insert into t_role value(#{id},#{name},#{keyword},#{description})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Integer add(Role role);

    @Delete("delete from t_role_menu where role_Id=#{id}")
    void deteleRoleAndMenu(Integer id);
    @Delete("delete from t_role_permission where role_Id=#{id}")
    void deteleRoleAndPermission(Integer id);
    @Delete("delete from t_role where id=#{id}")
    void deteleRole(Integer id);

    void edit(Role role);
}
